package com.nanxun.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanxun.domain.IndexImg;
import com.nanxun.feign.StoreProdFeign;
import com.nanxun.model.Result;
import com.nanxun.service.IndexImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * 轮播图业务控制层
 * @param
 * @return
 * @author nanxun
 * @create 2025/6/19
 **/
@Api(tags = "轮播图接口管理")
@RequestMapping("admin/indexImg")
@RestController
public class IndexImgController {

    @Resource
    private IndexImgService indexImgService;



    /**
     *
     * 多条件分页查询轮播图
     * @param current 页码
     * @param size 每页显示条数
     * @param status 状态
     * @return com.nanxun.model.Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.nanxun.domain.IndexImg>>
     * @author nanxun
     * @create 2025/6/19
     **/
    @ApiOperation("多条件分页查询轮播图")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public Result<Page<IndexImg>> loadIndexImgPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                                @RequestParam(required = false) Integer status
                                               ){
        //创建轮播图分页对象
        Page<IndexImg> page = new Page<>(current,size);
        //多条件分页查询轮播图
        page=indexImgService.page(page,new LambdaQueryWrapper<IndexImg>()
                .eq(ObjectUtil.isNotNull(status),IndexImg::getStatus,status)
                .orderByDesc(IndexImg::getSeq)
        );
        return Result.success(page);
    }

    /**
     *
     * 新增轮播图
     * @param indexImg 轮播图对象
     * @return com.nanxun.model.Result<java.lang.String>
     * @author nanxun
     * @create 2025/6/20
     **/
      @ApiOperation("新增轮播图")
      @PostMapping
      @PreAuthorize("hasAuthority('admin:indexImg:save')")
      public Result<String> saveIndexImg(@RequestBody IndexImg indexImg){
          boolean saved = indexImgService.saveIndexImg(indexImg);
          return Result.handle(saved);
      }


      /**
       *
       * 根据标识查询轮播图信息
       * @param imgId 轮播图标识
       * @return com.nanxun.model.Result<com.nanxun.domain.IndexImg>
       * @author nanxun
       * @create 2025/6/20
       **/
        @ApiOperation("根据标识查询轮播图信息")
        @GetMapping("info/{imgId}")
        @PreAuthorize("hasAuthority('admin:indexImg:info')")
        public Result<IndexImg> loadIndexImgInfo(@PathVariable Long imgId){
           // IndexImg byId = indexImgService.getById(imgId);//这里不能用getById，因为涉及到关联商品，需要自己定义一个方法
            IndexImg indexImg = indexImgService.queryIndexImgInfoById(imgId);
            return Result.success(indexImg);
        }


        /**
         *
         * 修改轮播图内容
         * @param indexImg 轮播图对象
         * @return com.nanxun.model.Result<java.lang.String>
         * @author nanxun
         * @create 2025/6/20
         **/

    @ApiOperation("修改轮播图内容")
    @PutMapping
    @PreAuthorize("hasAuthority('admin:indexImg:update')")
    public Result<String> modifyIndexImg(@RequestBody IndexImg indexImg){
        Boolean modified = indexImgService.modifyIndexImg(indexImg);
        return Result.handle(modified);
    }


    /**
     *
     * 批量删除轮播图
     * @param imgIds 轮播图id集合
     * @return com.nanxun.model.Result<java.lang.String>
     * @author nanxun
     * @create 2025/6/20
     **/
    @ApiOperation("批量删除轮播图")
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin:indexImg:delete')")
    public Result<String> removeIndexImg(@RequestBody List<Long> imgIds){
        Boolean removed = indexImgService.removeIndexImgByIds(imgIds);
        return Result.handle(removed);
    }


    //////////////////微信小程序 数据接口////////////////////////
    /**
     *
     * 查询小程序轮播图列表
     * @return com.nanxun.model.Result<java.util.List<com.nanxun.domain.IndexImg>>
     * @author nanxun
     * @create 2025/6/23
     **/

    //   admin/indexImg/indexImgs
    @ApiOperation("查询小程序轮播图列表")
    @GetMapping("indexImgs")
    public Result<List<IndexImg>> loadWxIndexImgList(){
        List<IndexImg> indexImgList = indexImgService.queryWxIndexImgList();
        return Result.success(indexImgList);
    }

}
